package sdc.com.petscalendar.adapters;

import java.util.ArrayList;
import java.util.Date;

import sdc.com.petscalendar.R;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class DayAdapter extends BaseAdapter {
	public Context context;
	private ArrayList<Date> days;
	private int month;

	@SuppressWarnings("deprecation")
	public DayAdapter(Context context, int month) {
		days = new ArrayList<Date>();
		this.context = context;
		this.month = month;

		// Get day's list
		Date d = new Date();
		d.setMonth(month);
		d.setDate(1);

		int count = 1;
		if (d.getDay() != 0) {
			for (int i = 0; i < d.getDay(); i++) {

				days.add(0, new Date(d.getTime() - 86400000L * count));
				count++;
			}
		}
		while (d.getMonth() == month) {
			days.add(d);
			d = new Date(d.getTime() + 86400000L);
		}
		while (d.getDay() != 0) {
			days.add(d);
			d = new Date(d.getTime() + 86400000L);
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return days.size();
	}

	@Override
	public Date getItem(int position) {
		// TODO Auto-generated method stub
		if (position >= 0 && position < days.size())
			return days.get(position);
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		if (convertView == null) {
			row = ((LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
					.inflate(R.layout.layout_day_item, parent, false);
		}
		row.setLayoutParams(new GridView.LayoutParams(parent.getWidth() / 7,
				parent.getWidth() / 7));
		Date d = getItem(position);
		TextView tvDay = (TextView) row.findViewById(R.id.tv_day_item);
		tvDay
				.setText((d.getDate()) + "");
		if (d.getMonth() != month) {
			tvDay.setEnabled(false);
			tvDay.setTextColor(context.getResources().getColor(R.color.day_disable));
			tvDay.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
		}else{
			tvDay.setEnabled(true);
			tvDay.setTextColor(context.getResources().getColor(R.color.day_normal));
			tvDay.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
		}
		return row;
	}
}
